﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    class WaitingFlies
    {
        private FliesMovement movement = FliesMovement.None;

        private Texture2D flySprites;

        private int tileSize = 64;

        private Random random;

        private double timeSinceLastGeneration = 0.0d;

        private List<Fly> waitingFlies;

        public WaitingFlies(Texture2D flySprites)
        {
            this.flySprites = flySprites;

            random = new Random();

            waitingFlies = new List<Fly>();
        }

        public Fly GetFly(int index)
        {
            return waitingFlies[index];
        }

        public void removeFly(int i)
        {
            waitingFlies.RemoveAt(i);
        }

        public void AddFly(int x,int y)
        {
            waitingFlies.Add(new Fly(flySprites, x, y, true));
        }

        public void Update(GameTime gameTime, Flies flies,Ennemies ennemies,bool isSlowMotion)
        {

            float realSpeed = Program.speedFlies;

            if (isSlowMotion)
                realSpeed = realSpeed / 2;

            for (int i = 0; i < waitingFlies.Count; i++)
            {
                waitingFlies[i].Update(gameTime, movement, realSpeed,isSlowMotion);

                if (waitingFlies[i].isOutOfGame())
                {
                    waitingFlies.RemoveAt(i);
                }
                else
                {
                    Fly fly;

                    for (int j = 0; j < flies.Count(); j++)
                    {
                        fly = flies.GetFly(j);

                        if (fly != null)
                        {
                            if (waitingFlies[i].GetDestination().Intersects(fly.GetDestination()))
                            {
                                flies.AddFly(new Fly(flySprites, waitingFlies[i].GetDestination().X, 
                                    waitingFlies[i].GetDestination().Y, false));

                                removeFly(i);
                                
                                break;
                            }
                        }
                    }
                }
            }

            timeSinceLastGeneration += gameTime.ElapsedGameTime.TotalSeconds;

            if (timeSinceLastGeneration >= 4.0)
            {
                timeSinceLastGeneration = 0.0f;

                int x=800;
                int y;

                do
                {
                    y = random.Next(600 - tileSize);
                }
                while (!collisionWithEnnemies(x, y, ennemies));
               
                AddFly(x,y);
            }
        }

        public Boolean collisionWithEnnemies(int x,int y,Ennemies ennemies)
        {
            for (int i = 0; i < ennemies.Count(); i++)
            {
                if (ennemies.GetEnnemy(i).GetDestination().Intersects(new Rectangle(x, y, tileSize, tileSize)))
                    return false;
            }

            return true;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Fly fly in waitingFlies)
            {
                fly.Draw(spriteBatch,Fly.type.civil);
            }
        }
    }
}
